package com.softpo.encryptiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    private String path = "http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";

    //毛泽东 == %E6%AF%9B%E6%B3%BD%E4%B8%9C
    private String url1 = "https://www.baidu.com/s?wd=毛泽东&ie=utf-8&tn=98050039_pg&ssl_s=1&ssl_c=ssl6_15899374a05";

    private String biluochun = "%E7%A2%A7%E8%9E%BA%E6%98%A5";
    private String tea = "http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=news.searcListByTitle&search=%E7%A2%A7%E8%9E%BA%E6%98%A5";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //URLEncode编码

//        https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&tn=98050039_pg&
// wd=%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89%E5%8D%83%E9%87%8C%E5%86%B0%E5%B0%81%E4%B8%87%E9%87%8C%E9%9B%AA%E9%A3%98
// &oq=%E6%AF%9B%E6%B3%BD%E4%B8%9C&rsv_pq=832fd1b400000bec&rsv_t=bbbfa5B8cTl4d6ihyU42w647crmXZhsZH3CmbCyUtkIH%2FH9kxH3wy8BnU0%2BxwhCeyyU&rqlang=cn&rsv_enter=1&rsv_sug3=16&rsv_sug1=14&rsv_sug7=100&rsv_sug2=1&prefixsug=%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89
// &rsp=0&inputT=7910&rsv_sug4=7911

        String encode = null;
        try {
            encode = URLEncoder.encode("毛泽东","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.d("flag", "----------------->onCreate: 进行URLEncod之后的内容： " +encode);

        //使用URLDecoder进行解码
        String str = "%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89%E5%8D%83%E9%87%8C%E5%86%B0%E5%B0%81%E4%B8%87%E9%87%8C%E9%9B%AA%E9%A3%98";


        String decode = null;
        try {
            decode = URLDecoder.decode(str,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.d("flag", "----------------->onCreate: 进行URDecoder之后的内容是： " +decode);

        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is = null;
                ByteArrayOutputStream baos = null;
                try {
                    URL url = new URL(tea);

                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setConnectTimeout(5000);

                    if(conn.getResponseCode() ==200){
                         is = conn.getInputStream();

                         baos = new ByteArrayOutputStream();

                        int len =0;

                        byte[] buf  = new byte[1024*8];

                        while ((len = is.read(buf))!=-1){
                            baos.write(buf,0,len);
                        }
                        byte[] bytes = baos.toByteArray();

                        Log.d("flag", "----------------->run: " +new String(bytes));
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Log.d("flag", "----------------->run: MalformedURLException");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("flag", "----------------->run: IOException");
                }finally {
                    Log.d("flag", "----------------->run: finally");
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}
